// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Material.h"

Material::Material(const QString &name) :
    m_name(name)
{
    setAmbient(QColor(128, 128, 128, 255));
    setDiffuse(QColor(204, 204, 204, 255));
    setSpecular(Qt::black);
    setEmission(Qt::black);
    setShininess(10.0);
}

Material::~Material()
{
}

void Material::paint()
{
    /* FIXME complete the code */
    // set up ambient, emission, diffuse, specular, shininess
    // material parameters
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_ambient); //set up ambient
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_diffuse); //set up diffuse
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_specular); //set up specular
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, m_emission); //set up  emission
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_shininess); //set up shininess
    /* STOP here */
}

Material::Material(const Material &other) :
    m_name(other.m_name),
    m_shininess(other.m_shininess)
{
    for (int i = 0; i < 4; i++) {
        m_ambient[i] = other.m_ambient[i];
        m_emission[i] = other.m_emission[i];
        m_diffuse[i] = other.m_diffuse[i];
        m_specular[i] = other.m_specular[i];
    }
}

Material &Material::operator =(const Material &other)
{
    m_name = other.m_name;
    m_shininess = other.m_shininess;
    for (int i = 0; i < 4; i++) {
        m_ambient[i] = other.m_ambient[i];
        m_emission[i] = other.m_emission[i];
        m_diffuse[i] = other.m_diffuse[i];
        m_specular[i] = other.m_specular[i];
    }

    return *this;
}

void Material::setAmbient(const QColor &ambient)
{
    m_ambient[0] = (GLfloat) ambient.redF();
    m_ambient[1] = (GLfloat) ambient.greenF();
    m_ambient[2] = (GLfloat) ambient.blueF();
    m_ambient[3] = (GLfloat) ambient.alphaF();
}

void Material::setEmission(const QColor &emission)
{
    m_emission[0] = (GLfloat) emission.redF();
    m_emission[1] = (GLfloat) emission.greenF();
    m_emission[2] = (GLfloat) emission.blueF();
    m_emission[3] = (GLfloat) emission.alphaF();
}

void Material::setDiffuse(const QColor &diffuse)
{
    m_diffuse[0] = (GLfloat) diffuse.redF();
    m_diffuse[1] = (GLfloat) diffuse.greenF();
    m_diffuse[2] = (GLfloat) diffuse.blueF();
    m_diffuse[3] = (GLfloat) diffuse.alphaF();
}

void Material::setSpecular(const QColor &specular)
{
    m_specular[0] = (GLfloat) specular.redF();
    m_specular[1] = (GLfloat) specular.greenF();
    m_specular[2] = (GLfloat) specular.blueF();
    m_specular[3] = (GLfloat) specular.alphaF();
}

void Material::setShininess(double shininess)
{
    m_shininess = (GLfloat) shininess;
}

QColor Material::getAmbient() const
{
    return QColor(m_ambient[0]*255.0,
                  m_ambient[1]*255.0,
                  m_ambient[2]*255.0,
                  m_ambient[3]*255.0);
}

QColor Material::getEmission() const
{
    return QColor(m_emission[0]*255.0,
                  m_emission[1]*255.0,
                  m_emission[2]*255.0,
                  m_emission[3]*255.0);
}

QColor Material::getDiffuse() const
{
    return QColor(m_diffuse[0]*255.0,
                  m_diffuse[1]*255.0,
                  m_diffuse[2]*255.0,
                  m_diffuse[3]*255.0);
}

QColor Material::getSpecular() const
{
    return QColor(m_specular[0]*255.0,
                  m_specular[1]*255.0,
                  m_specular[2]*255.0,
                  m_specular[3]*255.0);
}

GLfloat Material::getShininess() const
{
    return m_shininess;
}

const QString &Material::getName() const
{
    return m_name;
}


